package com.wectrls.service.impl;

import com.google.common.collect.Lists;
import com.wectrls.common.functionalInterface.FieldNameUtils;
import com.wectrls.config.exceptiom.CommonException;
import com.wectrls.entity.Category;
import com.wectrls.entity.Sources;
import com.wectrls.entity.WecsUser;
import com.wectrls.enums.RESPONSE_ENUM;
import com.wectrls.mapper.CategoryMapper;
import com.wectrls.service.CategoryService;
import com.wectrls.service.SourcesService;
import com.wectrls.service.WecsUserService;
import com.wectrls.vo.dto.CategoryDto;
import com.wectrls.vo.postvo.CategoryAddVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author chenjian
 * @Package
 * @Description:
 * @date 2019-10-31 0:24
 */
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SourcesService sourcesServiceImpl;

    @Autowired
    private WecsUserService wecsUserServiceImpl;


    /**
     * 通过ID查找
     *
     * @param categoryId
     * @return
     */
    @Override
    public Category findById(String categoryId) {
        Optional<Category> optional = categoryMapper.findById(categoryId);
        if (optional.isPresent()) {
            return optional.get();
        }
        throw new CommonException("无资源", 4004);
    }

    @Override
    public List<CategoryDto> findDtoByUserId(String wecsUserId) {
        Sort sort = Sort.by(FieldNameUtils.convertToFieldName(Category::getSortNum)).ascending();
        List<Category> categoryList = categoryMapper.findByWecsUserId(wecsUserId,sort);
        List<CategoryDto> categoryDtos = Lists.newArrayList();
        for (Category category : categoryList) {
            CategoryDto categoryDto = new CategoryDto();
            categoryDto.setCategory(category);
            Sort and = Sort.by(FieldNameUtils.convertToFieldName(Sources::getSortNum)).ascending()
                    .and(Sort.by(FieldNameUtils.convertToFieldName(Sources::getCTime)).ascending());
            List<Sources> sourcesList = sourcesServiceImpl.findByCategoryId(category.getId(),and);
            categoryDto.setSources(sourcesList);
            categoryDto.setSourcesCount(sourcesList.size());
            categoryDtos.add(categoryDto);
        }
        return categoryDtos;
    }

    @Override
    public Category addOne(CategoryAddVo categoryAddVo) {
        WecsUser wecsUser = wecsUserServiceImpl.findBySecurityUserName();
        Category category = new Category();
        category.setSortNum(categoryAddVo.getSortNum());
        category.setName(categoryAddVo.getName());
        category.setWecsUserId(wecsUser.getId());
        Category saveAndFlush = categoryMapper.saveAndFlush(category);
        return saveAndFlush;
    }

    @Override
    public Category saveOrUpdate(Category category) {
        Category saveAndFlush = categoryMapper.saveAndFlush(category);
        return saveAndFlush;
    }

    @Override
    public Category delCategoryById(String categoryId) {
        Optional<Category> optional = categoryMapper.findById(categoryId);
        Category category;
        if (optional.isPresent()) {
            category = optional.get();
            sourcesServiceImpl.delSourcesByCategoryId(category.getId());
            categoryMapper.deleteById(category.getId());
            return category;
        }
        throw new CommonException("删除数据失败", RESPONSE_ENUM.FAIL_500.getCode());
    }
}
